package com.zhuyp.shiro.realm;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;

public class MyRealm extends AuthorizingRealm {

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		String username = (String) principalCollection.getPrimaryPrincipal();
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		Set<String> set = new HashSet<String>();
		set.add("admin");
		authorizationInfo.setRoles(set);
		authorizationInfo.setStringPermissions(set);
		return authorizationInfo;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
			throws AuthenticationException {
		String username = (String) authenticationToken.getPrincipal();
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(  
				username, //用户名  
				"1e0450802eae1c4d01745b705cdb6cbd3bde61eace6ac3af3e5eebf584560fe4", //密码  
                getName()  //realm name  
        );
		doGetAuthorizationInfo(authenticationInfo.getPrincipals());
		return authenticationInfo;
	}
	
	public void removeUserAuthorizationInfoCache(String username) {
	    SimplePrincipalCollection pc = new SimplePrincipalCollection();  
	    pc.add(username, super.getName());
	    super.clearCachedAuthorizationInfo(pc);
	} 

}
